package com.atguigu.msmservice.controller;

import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.atguigu.commonutils.R;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.Demo鼎信短信平台;
import com.atguigu.msmservice.utils.HttpApiClient鼎信短信平台;
import com.atguigu.msmservice.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RequestMapping("/edumsm/msm")
@RestController
@CrossOrigin
public class MsmController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone) {
        //从redis获取验证码，如果获取到就直接返回
        String s = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(s)){
            return R.ok();
        }else {
            try{
                //取不到就发送
                String paramNumber= RandomUtil.getFourBitRandom();
                String param="code:"+paramNumber;
                Demo鼎信短信平台.鼎信科技_短信验证码_短信通知APIHttpSyncTest(phone, param, "TP1711063");
                redisTemplate.opsForValue().set(phone,paramNumber,60, TimeUnit.MINUTES);
                return R.ok();
            }catch (Exception e){
                e.printStackTrace();
                return R.error();
            }

        }

    }

}
